using System;
using CSW.Framework.Common;
using CSW.Framework.Common.Utils.StringUtils;

namespace CSW.Framework.UI.Binding
{
    /// <summary>
    /// DisplayConverter.
    /// </summary>
    public class DisplayConverter
    {
        private readonly string m_DisplayText;
        private readonly UnformatTextDelegate m_ConvertFromDisplay;

        /// <summary>
        /// Occurs when the display needs to be updated.
        /// </summary>
        public virtual event EventHandler UpdateDisplay;

        /// <summary>
        /// Initializes a new instance of the <see cref="DisplayConverter"/> class.
        /// </summary>
        /// <param name="displayFormat">The display format.</param>
        public DisplayConverter(string displayFormat)
        {
            Guard.ArgumentNotNullOrEmptyString(displayFormat, "displayFormat");

            m_DisplayText = "{0:" + displayFormat + "}";

            if (displayFormat == "c")
            {
                m_ConvertFromDisplay = (displayText => StringConverter.GetCurrencyAsDoubleOrNull(displayText).ToString());
            }
            else if (displayFormat == "n" || displayFormat.Contains(","))
            {
                m_ConvertFromDisplay = delegate(string displayText)
                                           {
                                               if (string.IsNullOrEmpty(displayText))
                                                   return displayText;
                                               else
                                                    return displayText.Replace(",", "");
                                           };
            }
            else if (displayFormat.EndsWith("%"))
            {
                m_ConvertFromDisplay = (displayText => StringConverter.GetCurrencyAsDoubleOrNull(displayText).ToString());
            }
            else
            {
                m_ConvertFromDisplay = (displayText => displayText);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DisplayConverter"/> class.
        /// </summary>
        protected DisplayConverter()
        {
        }

        /// <summary>
        /// Gets the formatted text.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The formatted text.</returns>
        public virtual string GetFormattedText(object value)
        {
            return string.Format(m_DisplayText, value);
        }

        /// <summary>
        /// Gets the unformatted text.
        /// </summary>
        /// <param name="displayText">The display text.</param>
        /// <returns>The unformatted text.</returns>
        public virtual string GetUnformattedText(string displayText)
        {
            return m_ConvertFromDisplay(displayText);
        }

        /// <summary>
        /// Fires the UpdateDisplay event.
        /// </summary>
        protected void FireUpdateDisplay()
        {
            EventHandler updateDisplay = UpdateDisplay;
            if (updateDisplay != null)
                updateDisplay(this, EventArgs.Empty);
        }
    }
}